package com.mystore.framework.service.impl;

import com.mystore.common.utils.string.RedisKeyStringUtils;
import com.mystore.framework.service.RedisService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

public class RedisServiceImpl implements RedisService {
    protected RedisTemplate<String,Object> redisTemplate;
    @Override
    public void put(String key, Object value,String suffix,long timeOut,TimeUnit timeUnit) {
        String trulyKey= RedisKeyStringUtils.buildKey(suffix,key);
        ValueOperations<String,Object> operations =redisTemplate.opsForValue();
        operations.set(trulyKey,value);
        redisTemplate.expire(trulyKey,timeOut, timeUnit);
    }

    @Override
    public Object get(String key,String suffix) {
        ValueOperations<String,Object> operations =redisTemplate.opsForValue();
        return operations.get(RedisKeyStringUtils.buildKey(suffix,key));
    }

    @Override
    public void delete(String key,String suffix) {
        redisTemplate.delete(RedisKeyStringUtils.buildKey(suffix,key));
    }
}
